# Simple, good-enough but not 100% perfect program that reads comun source code
# from input and outputs it with a syntax highlight as an HTML.

0 "<html><head><style>" --> 10 ->
0 "  .str { color: #0d3; }" --> 10 ->
0 "  .comm { color: #888; }" --> 10 ->
0 "  .prep { color: #00f; }" --> 10 ->
0 "  .ptr { color: #b80; }" --> 10 ->
0 "  .dir { color: #08e; }" --> 10 ->
0 "  .name { color: #da0; }" --> 10 ->
0 "  .con { color: #f00; }" --> 10 ->
0 "</style></head><body><pre>" --> 10 ->

STATE_CLEAR: 0 .
STATE_STRING: 1 .
STATE_COMMENT: 2 .
STATE_SPECIAL: 3 .

outputChar:
  $0 ">" = ?
    0 "&gt;" --> ^
  ;
  $0 "<" = ?
    0 "&lt;" --> ^
  ;
    ->
  . .
.

processChar: # in: state, char
  $0 91 = $1 93 = | ?
    0 "<span class='prep'>" --> -> 0 "</span>" -->
    !.
  .

  $1 STATE_STRING = ?
    $0 outputChar

    34 = ? # double quote
      0 "</span>" -->
      STATE_CLEAR $:1
    .
    !.
  .
  $1 STATE_COMMENT = ?
    $0 10 = ? # newline
      0 "</span>" -->
      STATE_CLEAR $:2
    .

    $0 outputChar

    "#" = ?
      0 "</span>" -->
      STATE_CLEAR $:0
    .
    !.
  .
  $1 STATE_SPECIAL = ?
    $0 " " = $1 10 = | ?
      0 "</span>" -->
      STATE_CLEAR $:2
    .
    outputChar
    !.
  .

  # STATE_CLEAR:
  $0 34 = ? # double quote
    0 "<span class='str'>" -->
    STATE_STRING $:2
  ;
  $0 "#" = ?
    0 "<span class='comm'>" -->
    STATE_COMMENT $:2
  ;
  $0 "$" = ?
    0 "<span class='ptr'>" -->
    STATE_SPECIAL $:2
  ;
  $0 "~" = ?
    0 "<span class='dir'>" -->
    STATE_SPECIAL $:2
  ;
  $0 "?" = $1 "@" = | $1 "." = | $1 ";" = | ?
    0 "<span class='con'>" -->
    STATE_SPECIAL $:2
  ;
  $0 "a" >= $1 "z" <= & $1 "A" >= $2 "Z" <= & | $1 "_" = | ?
    0 "<span class='name'>" -->
    STATE_SPECIAL $:2
  ;
  $0 " " != $1 10 != & ?
    STATE_SPECIAL $:2
  . . . . . . .

  outputChar
.

STATE_CLEAR

@@
  <-

  $0 0 = <? !! | ?
    !@
  .
 
  processChar
.

0 "</pre></body></html>" --> 10 ->
